package com.example.lms.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lms.entity.ShipmentRecords;
import com.example.lms.entity.Warehouses;
import com.example.lms.mapper.WarehousesMapper;
import com.example.lms.service.ShipmentRecordsService;
import com.example.lms.service.WarehousesService;
import com.example.lms.vo.D;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @module
 * @author：DUOLUONIANDAI
 * @DATA：2024/05/11
 * @Title：
 */

@Service
public class WarehousesServiceImp extends ServiceImpl<WarehousesMapper, Warehouses> implements WarehousesService {
    @Override
    public D addWarehousesInfo(Warehouses warehouses) {
        warehouses.setId(UUID.randomUUID().toString());
        save(warehouses);
        D build = D.builder().build();
        build.inputVal("保存仓库信息",warehouses);

        return build;
    }

    @Override
    public D getWarehousesInfo(String id) {
        Warehouses id1 = getOne(new QueryWrapper<Warehouses>().eq("id", id));

        D build = D.builder().build();

        if (ObjectUtils.isEmpty(id1)){
            build.inputVal("仓库id不存在",id);
        }else {
            build.inputVal("仓库信息为：",id1);
        }
        return build;
    }

    @Resource
    ShipmentRecordsService shipmentRecordsService;

    @Override
    public D delWareHouses(String id) {
        Warehouses id1 = getOne(new QueryWrapper<Warehouses>().eq("id", id));

        D build = D.builder().build();

        if (ObjectUtils.isEmpty(id1)){
            build.inputVal("仓库信息不存在",id1);
            return build;
        }
        List<ShipmentRecords> warehouses_id = shipmentRecordsService.list(new QueryWrapper<ShipmentRecords>().eq("warehouses_id", id));

        boolean id2 = remove(new QueryWrapper<Warehouses>().eq("id", id));
        if (id2){
            build.inputVal("删除成功！",id1);
            build.inputVal("顺带删除的引用",warehouses_id);
        }else {
            build.inputVal("删除失败！",id1);
            build.inputVal("顺带删除的引用",warehouses_id);
        }

        return build;
    }

    @Override
    public D uploadWareHouses(Warehouses warehouses) {
        Warehouses id = getOne(new QueryWrapper<Warehouses>().eq("id", warehouses.getId()));

        D build = D.builder().build();

        if (ObjectUtils.isEmpty(id)){
            build.inputVal("仓库不存在",warehouses);
            return build;
        }

        if(ObjectUtils.isEmpty(warehouses.getCapacity())){
            warehouses.setCapacity(id.getCapacity());
        }
        if (ObjectUtils.isEmpty(warehouses.getLocation())){
            warehouses.setLocation(warehouses.getLocation());
        }

        boolean b = updateById(warehouses);

        build.inputVal("旧数据",id);
        build.inputVal("新数据",warehouses);

        return build;
    }


}
